<?php
require_once ("secure_area.php");
class Reports extends Secure_area 
{
	function __construct()
	{
		parent::__construct('reports');
		$this->load->helper('report');		
	}
	
	//Initial report listing screen
	function index()
	{
		$this->load->view("reports/listing",array());	
	}
	
	function _get_common_report_data()
	{
		$data = array();
		$data['report_date_range_simple'] = get_simple_date_ranges();
		$data['months'] = get_months();
		$data['days'] = get_days();
		$data['years'] = get_years();
		$data['selected_month']=date('n');
		$data['selected_day']=date('d');
		$data['selected_year']=date('Y');	
	
		return $data;
	}
	
	
	//Input for reports that require only a date range. (see routes.php to see that all summary reports route here)
	function date_input()
	{
		$data = $this->_get_common_report_data();
		$data['manage']=staffs_report_manage_table($this->Staff->get_all(),$this);
		$this->load->view("reports/date_input",$data);	
	}
	
	/////////////////// mek ///////////////////************************************************
	
	//pay_slip
	function pay_slip($payroll_date, $staff_ids)
	{
		$this->load->model('reports/Pay_slip');
		$model = $this->Pay_slip;
		$date_array = explode(',',$payroll_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		
		$report_data = $model->getData(array('year'=>$year, 'month'=>$month,'staff_ids'=>$staff_ids));
		$sortable_data = array();
		
		$all_totals = array('basic_wages'=>0,'gross_pay'=>0,'net_pay'=>0,'total_ssf_payable'=>0);
		foreach($report_data->result() as $row)
		{
			$raw_data = array();
			$staff_info = $this->Staff->get_info($row->person_id);
			$payroll_info = $this->get_payroll_info($row->payroll_id,'compute');
			
			$all_totals['basic_wages'] += $payroll_info['basic_wages'];
			$all_totals['gross_pay'] += $payroll_info['gross_pay'];
			$all_totals['net_pay'] += $payroll_info['net_pay'];
			$all_totals['total_ssf_payable'] += $payroll_info['total_ssf_payable'];
			
			$raw_data[] = $payroll_info['payroll_id'];
			$raw_data[] = ucwords(strtolower($staff_info->last_name.' '.$staff_info->first_name));
			$raw_data[] = $payroll_info['basic_wages'];
			$raw_data[] = $payroll_info['gross_pay'];
			$raw_data[] = $payroll_info['net_pay'];
			$raw_data[] = $payroll_info['total_ssf_payable'];
			
			
			$sortable_data[] = $raw_data;	
		}
		$data['manage']= manage_report_payroll_table($sortable_data,$this,$all_totals);
		$data['day']= $day;
		$data['month']= $month;
		$data['year']= $year;
		$data['payroll_date']= $payroll_date;
		$data['staff_ids']= $staff_ids;
		$data['controller']=strtolower($this->uri->segment(1));

		$this->load->view("reports/pay_slip",$data);
	}
	
	//Pritning of the all the Staff Payroll at Once
	function print_all_pay_slip($payroll_date, $staff_ids)
	{
		$this->load->model('reports/Pay_slip');
		$model = $this->Pay_slip;
		$date_array = explode(',',$payroll_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		
		$report_data = $model->getData(array('year'=>$year, 'month'=>$month,'staff_ids'=>$staff_ids));
		$sortable_data = array();
		
		$all_print = "";
		foreach($report_data->result() as $row)
		{
			//$payroll_info = $this->get_payroll_info($row->payroll_id);
			$all_print .= $this->process_pay_slip($row->payroll_id);	
		}
		
		$data['manage']= $all_print;
		$data['controller']=strtolower($this->uri->segment(1));

		$this->load->view("reports/print_payroll", $data);
	}
	
	//To print a payroll for a month
	function process_pay_slip($payroll_id)
	{
		$data = array();
		$data = $this->get_payroll_info($payroll_id,'compute');
		$person_info=$this->Staff->get_info($data['staff_id']);
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		//Allowance
		$allowance = array();
		foreach($payroll_info['query_allowance']->result_array() as $row)
		{
			$str = $row['taxable'] == '0' ? $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( not taxed )': $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( taxed )';
			$allowance[$str] = doubleval($row['amount']) ; 	
		}
		//Deductions
		$deductions = array();
		foreach($payroll_info['query_deductions']->result_array() as $row)
		{
			$deductions[$row['name']] = doubleval($row['amount']) ; 	
		}
		
		$data['allowance']=$allowance;
		$data['deductions']=$deductions;
		$data['controller']=strtolower($this->uri->segment(1));
		
		return all_payroll_print($data,$person_info,$this);
	}
	
	
	//Bank Advance
	
	function bank_relay()
	{
		$selected = $this->input->post('bank_type')=='' ? -1:$this->input->post('bank_type');
		$payroll_date = $this->input->post('payroll_date')=='' ? null:$this->input->post('payroll_date');
		$staff_ids = $this->input->post('staff_ids')=='' ? null:$this->input->post('staff_ids');
		$report_type = $this->input->post('report_type')=='' ? null:$this->input->post('report_type');
		
		if($report_type == 'bnk_all')
		{
			$this->bank_adv_all($payroll_date, $staff_ids,$selected);
		}
		else if($report_type == 'bnk_tax_only')
		{
			$this->bank_adv_tax_only($payroll_date, $staff_ids,$selected);
		}
		else if($report_type == 'bnk_tax_plus_ssf')
		{
			$this->bank_adv_tax_plus_ssf($payroll_date, $staff_ids,$selected);
		}
		else if($report_type == 'bnk_tax_no_ssf')
		{
			$this->bank_adv_tax_no_ssf($payroll_date, $staff_ids,$selected);
		}
		else if($report_type == 'bnk_no_tax_only')
		{
			$this->bank_adv_no_tax_only($payroll_date, $staff_ids,$selected);
		}
		else if($report_type == 'bnk_no_tax_plus_ssf')
		{
			$this->bank_adv_no_tax_plus_ssf($payroll_date, $staff_ids,$selected);
		}
		else if($report_type == 'bnk_no_tax_no_ssf')
		{
			$this->bank_adv_no_tax_no_ssf($payroll_date, $staff_ids,$selected);
		}		
	}
	
	function bank_adv_all($payroll_date, $staff_ids,$selected=-1)
	{
		$this->bank_adv('bnk_all',$payroll_date, $staff_ids, $selected);
	}
	
	function bank_adv_tax_only($payroll_date, $staff_ids,$selected=-1)
	{
		$this->bank_adv('bnk_tax_only',$payroll_date, $staff_ids, $selected);
	}
	
	function bank_adv_tax_plus_ssf($payroll_date, $staff_ids,$selected=-1)
	{
		$this->bank_adv('bnk_tax_plus_ssf',$payroll_date, $staff_ids, $selected);
	}
	
	function bank_adv_tax_no_ssf($payroll_date, $staff_ids,$selected=-1)
	{
		$this->bank_adv('bnk_tax_no_ssf',$payroll_date, $staff_ids, $selected);
	}
	
	function bank_adv_no_tax_only($payroll_date, $staff_ids,$selected=-1)
	{
		$this->bank_adv('bnk_no_tax_only',$payroll_date, $staff_ids, $selected);
	}
	
	function bank_adv_no_tax_plus_ssf($payroll_date, $staff_ids,$selected=-1)
	{
		$this->bank_adv('bnk_no_tax_plus_ssf',$payroll_date, $staff_ids, $selected);
	}
	
	function bank_adv_no_tax_no_ssf($payroll_date, $staff_ids,$selected=-1)
	{
		$this->bank_adv('bnk_no_tax_no_ssf',$payroll_date, $staff_ids, $selected);
	}
	
	
	function bank_adv($x,$payroll_date, $staff_ids,$selected)
	{
		$this->load->model('reports/Bank_adv');
		$model = $this->Bank_adv;
		$date_array = explode(',',$payroll_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		$particular_bank_name = '';
		
		$banks = array('-1' => $this->lang->line('bank_all'),'0' => $this->lang->line('bank_none'));
		foreach($this->Bank->get_all()->result_array() as $row)
		{
			$bank_id = $row['bank_id'];
			$bank_name = $row['name'];
			
			$banks[$bank_id] = $bank_name ;
			eval("\$bnk".$bank_id." = array();");
		}
		
		
		$report_data = $model->getData(array('year'=>$year, 'month'=>$month,'staff_ids'=>$staff_ids,'type'=>$x));
		
		$sortable_data = array();
		
		$all_totals = array('net_pay'=>0);
		foreach($report_data->result() as $row)
		{
			$raw_data = array();
			$staff_info = $this->Staff->get_info($row->person_id);
			$staff_bank_type = $staff_info->bank_type;
			
			if($selected == -1)//All Banks
			{
					$payroll_info = $this->get_payroll_info($row->payroll_id,'compute');
			
					$all_totals['net_pay'] += $payroll_info['net_pay'];
			
					$raw_data[] = ucwords(strtolower($staff_info->last_name.' '.$staff_info->first_name));
					$raw_data[] = $staff_info->bank_account_number;
					$raw_data[] = $payroll_info['net_pay'];
					eval("\$bnk".$staff_bank_type."[] = \$raw_data ;");
			}
			else
			{
				if($selected == $staff_bank_type)
				{
					$particular_bank_name = $this->Bank->get_info($selected)->name;
					$payroll_info = $this->get_payroll_info($row->payroll_id,'compute');
			
					$all_totals['net_pay'] += $payroll_info['net_pay'];
			
					$raw_data[] = ucwords(strtolower($staff_info->last_name.' '.$staff_info->first_name));
					$raw_data[] = $staff_info->bank_account_number;
					$raw_data[] = $payroll_info['net_pay'];
			
					$sortable_data[] = $raw_data;	
				}
			}
			
		}
		
		$data['day']= $day;
		$data['month']= $month;
		$data['year']= $year;
		$data['heading']= $this->lang->line('reports_'.$x);
		$data['payroll_date']= $payroll_date;
		$data['staff_ids']= $staff_ids;
		$data['report_type']= $x;		
		$data['banks']=$banks;
		$data['selected']= $selected;
		
		$heading = $this->lang->line('reports_'.$x);
		
		if($selected == -1)//All Banks
		{
			$all_str = '';
			foreach($this->Bank->get_all()->result_array() as $row)
			{
				$bank_id = $row['bank_id'];
				$bank_name = $row['name'];
				$eval_str = eval("return \$bnk".$bank_id.";");
				$tbl_str = manage_bank_adv_table($eval_str,$this,$all_totals);				
				$all_str .= print_all_bank($tbl_str,$this,$bank_name,$heading,$year,$month);
			}
			$data['manage']= $all_str;
		}
		else
		{
			$data['manage']= print_all_bank(manage_bank_adv_table($sortable_data,$this,$all_totals),$this,$particular_bank_name,$heading,$year,$month);
		}
		

		$this->load->view("reports/bank_adv",$data);
	}
	
	//SSF
	function ssf($x, $payroll_date, $staff_ids)
	{
		$this->load->model('reports/SSF');
		$model = $this->SSF;
		$date_array = explode(',',$payroll_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		
		$report_data = $model->getData(array('year'=>$year, 'month'=>$month,'staff_ids'=>$staff_ids,'type'=>$x));
		$sortable_data = array();
		
		$all_totals = array('basic_wages'=>0,'total_ssf_string'=>0);
		$inner_values = array();
		foreach($report_data->result() as $row)
		{
			$raw_data = array();
			$staff_info = $this->Staff->get_info($row->person_id);	
			
			$payroll_info = $this->get_payroll_info($row->payroll_id,'present');
			
			$all_totals['basic_wages'] += $payroll_info['basic_wages'];
		
			if(is_array($payroll_info['ssf_percent_value_pair']))
			{
				$all_keys = 0;
				$all_values = 0;
				$total_ssf_string = '';
				foreach($payroll_info['ssf_percent_value_pair'] as $key => $value)
				{
					//$all_keys = $all_keys + $key;
					$all_values = $all_values + $value;
				}
				$total_ssf_string = $all_values;
			}
			else
			{
				$total_ssf_string  = 0;
			}
			
			if(is_array($payroll_info['ssf_percent_value_pair']))
			{
				foreach($payroll_info['ssf_percent_value_pair'] as $key => $value)
				{
					$inner_values[] += $value;
				}
			}
			
			$all_totals['total_ssf_string'] += $total_ssf_string;
			
			$raw_data[] = ucwords(strtolower($staff_info->last_name.' '.$staff_info->first_name));
			$raw_data[] = $staff_info->social_security_no;
			$raw_data[] = $payroll_info['basic_wages'];
			$raw_data[] = $payroll_info['ssf_percent_value_pair'] ;
			
			$sortable_data[] = $raw_data;	
		}
		
		$all_totals['inner_values'] = $inner_values;
		
		$data['manage']= manage_ssf_table($sortable_data,$this,$all_totals);
		$data['day']= $day;
		$data['month']= $month;
		$data['year']= $year;
		$data['heading']= $this->lang->line('reports_'.$x);

		$this->load->view("reports/ssf",$data);
	}
	
	function ssf_all($payroll_date, $staff_ids)
	{
		$this->ssf('ssf_all',$payroll_date, $staff_ids);
	}
	
	function ssf_only($payroll_date, $staff_ids)
	{
		$this->ssf('ssf_only',$payroll_date, $staff_ids);
	}
	
	function ssf_no($payroll_date, $staff_ids)
	{
		$this->ssf('ssf_no',$payroll_date, $staff_ids);
	}	
	
	
	
	//IRS
	function irs($x, $payroll_date, $staff_ids)
	{
		$this->load->model('reports/IRS');
		$model = $this->IRS;
		$date_array = explode(',',$payroll_date);
		$_1 = explode(' ',$date_array[0]);
		$day = $_1[1];
		$month = $_1[0];
		$year = trim($date_array[1]);
		
		$report_data = $model->getData(array('year'=>$year, 'month'=>$month,'staff_ids'=>$staff_ids,'type'=>$x));
		$sortable_data = array();
		$all_totals = array('basic_wages'=>0,'allowance_totals'=>0,'emolument'=>0,'total_ssf_payable'=>0,'deduction_totals'=>0,'taxable_income'=>0,'tax'=>0);
		foreach($report_data->result() as $row)
		{
			$raw_data =array();
			$staff_info = $this->Staff->get_info($row->person_id);
			$payroll_info = $this->get_payroll_info($row->payroll_id,'compute');
			
			$all_totals['basic_wages'] += $payroll_info['basic_wages'];
			$all_totals['allowance_totals'] += $payroll_info['allowance_totals'];
			$emolument = $payroll_info['basic_wages']+ $payroll_info['allowance_totals'];
			$all_totals['emolument'] += $emolument;
			$all_totals['total_ssf_payable'] += $payroll_info['total_ssf_payable'];
			$all_totals['deduction_totals'] += $payroll_info['deduction_totals'];
			$all_totals['taxable_income'] += $payroll_info['taxable_income'];
			$all_totals['tax'] += $payroll_info['tax'];
			
			$raw_data[] = ucwords(strtolower($staff_info->last_name.' '.$staff_info->first_name));
			$raw_data[] = $staff_info->position;	
			$raw_data[] = $payroll_info['basic_wages'];
			$raw_data[] = $payroll_info['allowance_totals'];
			$emolument = $payroll_info['basic_wages']+ $payroll_info['allowance_totals'];
			$raw_data[] = $emolument;
			$raw_data[] = $payroll_info['total_ssf_payable'];
			$raw_data[] = $payroll_info['deduction_totals'];
			$raw_data[] = $payroll_info['taxable_income'];
			$raw_data[] = $payroll_info['tax'];
			
			$sortable_data[] = $raw_data;	
		}
		$data['manage']= manage_irs_table($sortable_data,$this,$all_totals);
		$data['day']= $day;
		$data['month']= $month;
		$data['year']= $year;
		$data['heading']= $this->lang->line('reports_'.$x);

		$this->load->view("reports/irs",$data);
	}
	
	
	function irs_all($payroll_date, $staff_ids)
	{
		$this->irs('irs_all',$payroll_date, $staff_ids);
	}
	
	function irs_tax_only($payroll_date, $staff_ids)
	{
		$this->irs('irs_tax_only',$payroll_date, $staff_ids);
	}
	
	function irs_tax_plus_ssf($payroll_date, $staff_ids)
	{
		$this->irs('irs_tax_plus_ssf',$payroll_date, $staff_ids);
	}
	
	function irs_tax_no_ssf($payroll_date, $staff_ids)
	{
		$this->irs('irs_tax_no_ssf',$payroll_date, $staff_ids);
	}
	
	function irs_no_tax_only($payroll_date, $staff_ids)
	{
		$this->irs('irs_no_tax_only',$payroll_date, $staff_ids);
	}
	
	function irs_no_tax_plus_ssf($payroll_date, $staff_ids)
	{
		$this->irs('irs_no_tax_plus_ssf',$payroll_date, $staff_ids);
	}
	
	function irs_no_tax_no_ssf($payroll_date, $staff_ids)
	{
		$this->irs('irs_no_tax_no_ssf',$payroll_date, $staff_ids);
	}
	
	
	//***************************************************************************************//
	
	
	//Pritning of the list of staff
	function print_staff()
	{
		$data['manage'] = staffs_print_manage_table($this->Staff->get_all(),$this);
		$this->load->view("reports/print_staff",$data);	
	}
	
	function detailed_payroll($payroll_id)
	{
		$data = array();
		$data = $this->get_payroll_info($payroll_id,'compute');
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		//Allowance
		$allowance = array();
		foreach($payroll_info['query_allowance']->result_array() as $row)
		{	
			$str = $row['taxable'] == '0' ? $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( not taxed )': $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( taxed )';
			$allowance[$str] = $row['amount'] ; 
		}
		//Deductions
		$deductions = array();
		foreach($payroll_info['query_deductions']->result_array() as $row)
		{
			$deductions[$row['name']] = $row['amount'] ; 	
		}
		
		$data['allowance']=$allowance;
		$data['deductions']=$deductions;
		$this->load->view("staffs/detailed_payroll", $data);
	}
	
	//To print a payroll for a month
	function print_payroll($payroll_id)
	{
		$data = array();
		$data = $this->get_payroll_info($payroll_id,'compute');
		$data['person_info']=$this->Staff->get_info($data['staff_id']);
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		//Allowance
		$allowance = array();
		foreach($payroll_info['query_allowance']->result_array() as $row)
		{
			$str = $row['taxable'] == '0' ? $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( not taxed )': $row['name'].'&nbsp;&nbsp;&nbsp;&nbsp; ( taxed )';
			$allowance[$str] = $row['amount'] ; 	
		}
		//Deductions
		$deductions = array();
		foreach($payroll_info['query_deductions']->result_array() as $row)
		{
			$deductions[$row['name']] = $row['amount'] ; 	
		}
		
		$data['allowance']=$allowance;
		$data['deductions']=$deductions;
		$data['controller']=strtolower($this->uri->segment(1));
		
		$this->load->view("staffs/print_payroll", $data);
	}
	
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 360;
	}
	
	///////////////////////**************************** FROM STAFF CONTROLLER ******************************//////////////////////////////////////////////////////
	
	//Get a detailed_payroll
	function get_payroll_info($payroll_id=-1,$ssf_type)
	{		
		$payroll_info = $this->Staff->get_payroll_for_editing($payroll_id);
		$payroll = $payroll_info['query_payroll']->row();
		$date = $payroll->date;
		$year = $payroll->year;
		$month = $payroll->month;
		$day = $payroll->day;
		$tax_cat = $payroll->tax_cat;
		$ssf_cat = $payroll->ssf_cat;
		$basic_wages = $payroll->basic_wages;
		$staff_id = $payroll->person_id;
		
		//Get the total  allawance
		$allowance_result = $this->Staff->get_allowance_totals($payroll_id);
		if($allowance_result->num_rows() > 0)
		{
			$allowance_row = $allowance_result->row();
			$allowance_totals = $allowance_row->payroll_total;
			if(empty($allowance_totals))
			{
				$allowance_totals = 0;
			}
		}
		else
		{
			$allowance_totals = 0;
		}
		$allowance_totals = number_format($allowance_totals, 2, '.', '');
		
		//Get the total  non taxable allawance
		$no_tax_allowance_result = $this->Staff->get_non_taxable_allowance_totals($payroll_id);
		if($no_tax_allowance_result->num_rows() > 0)
		{
			$no_tax_allowance_row = $no_tax_allowance_result->row();
			$no_tax_allowance_totals = $no_tax_allowance_row->payroll_total;
			if(empty($no_tax_allowance_totals))
			{
				$no_tax_allowance_totals = 0;
			}
		}
		else
		{
			$no_tax_allowance_totals = 0;
		}
		$no_tax_allowance_totals = number_format($no_tax_allowance_totals, 2, '.', '');
		
			
			///////Get the total deductions//////
		$deductions_result = $this->Staff->get_deduction_totals($payroll_id);
		if($deductions_result->num_rows() > 0)
		{
			$deductions_row = $deductions_result->row();
			$deduction_totals = $deductions_row->payroll_total;
			if(empty($deduction_totals))
			{
				$deduction_totals = 0;
			}
		}
		else
		{
			$deduction_totals = 0;
		}
		$deduction_totals = number_format($deduction_totals, 2, '.', '');
		
		////////////*************************** Now Do Tax and SS and Others**************************** ////////////
		
			$all_calc = $this->all_calc($basic_wages,$allowance_totals,$ssf_cat,$tax_cat,$deduction_totals,$no_tax_allowance_totals,$ssf_type);
			
			/////////////////////////////////////////********************************************////////////////////////////////////\
			$data['staff_id'] = $staff_id;
			$data['payroll_id'] = $payroll_id;
			$data['date'] = $date;
			$data['basic_wages'] = $basic_wages;
			$data['allowance_totals'] = $allowance_totals;
			
			$data['ssf_array'] = $all_calc['ssf_array'];
			
			$data['net_basic_wage'] = $all_calc['net_basic_wage'];
			$data['gross_pay'] = $all_calc['gross_pay'];
			$data['tax'] = $all_calc['tax'];
			$data['taxable_income'] = $all_calc['taxable_income'];
			$data['deduction_totals'] = $deduction_totals;
			$data['net_pay'] = $all_calc['net_pay'];
			$data['total_payroll_deductions'] = $all_calc['total_payroll_deductions'];
			$data['total_ssf_payable'] = $all_calc['total_ssf_payable'];
			$data['default_ssf_percent_value'] = $all_calc['default_ssf_percent_value'];
			$data['default_ssf_value'] = $all_calc['default_ssf_value'];
			$data['ssf_percent_value_pair'] = $all_calc['ssf_percent_value_pair'];
			
				
		return $data;
	}
	
	///////////////////CALCULATING FUNCTIONS///////////////////////////
	
	//CAll the calculating functions
	function all_calc($basic_wages,$allowance_totals,$ssf_cat,$tax_cat,$deduction_totals,$no_tax_allowance_totals,$ssf_type)
	{
		$result = array();
		//Get the default ssf percent value
		$default_ssf_value = 			$this->cal_default_ssf($ssf_cat,$basic_wages);
		//Calc Gross Pay
		$gross_pay = 					$this->cal_gross_pay($basic_wages,$allowance_totals);
		//Calc Net Basic Wage
		$net_basic_wage = 				$this->cal_net_basic_wage($basic_wages,$default_ssf_value);
		//Calc SSF
		$ssf_array = 					$this->cal_ssf($ssf_cat,$basic_wages);
		//Taxable Income
		$taxable_income = 				$this->cal_taxable_income($gross_pay,$default_ssf_value) - $no_tax_allowance_totals;
		//Tax
		$tax = 							$this->cal_tax($tax_cat,$taxable_income);
		//Total Deductions
		$total_payroll_deductions = 	$this->cal_total_payroll_deductions($tax,$deduction_totals);
		//Net Pay
		$net_pay = 						$this->cal_net_pay($taxable_income,$total_payroll_deductions) + $no_tax_allowance_totals;
		//Total SSF Payable
		$total_ssf_payable = 			$this->cal_total_ssf_payable($ssf_array);
		//Get the default ssf percent value
		$default_ssf_percent_value = 	$this->default_ssf_percent_value($ssf_cat);
		
		if($ssf_type == 'compute')
		{
			//SSFs Percent and Value Pair
			$ssf_percent_value_pair = 		$this->cal_ssf_percent_value_pair($ssf_cat,$basic_wages);
		}
		else
		{
			//SSFs Percent and Value Pair
			$ssf_percent_value_pair = 		$this->cal_ssf_percent_value_pair_presentation($ssf_cat,$basic_wages);
			//$ssf_percent_value_pair = 		$this->cal_ssf_percent_value_pair($ssf_cat,$basic_wages);
		}
		
		
		$result['gross_pay'] = 	$gross_pay;				
		$result['net_basic_wage'] = $net_basic_wage	;		
		$result['ssf_array'] = 	$ssf_array;			
		$result['taxable_income'] = $taxable_income	;			
		$result['tax'] = 	$tax;						
		$result['net_pay'] = 	$net_pay;					
		$result['total_payroll_deductions'] = 	$total_payroll_deductions;
		$result['total_ssf_payable'] = 	$total_ssf_payable;
		$result['default_ssf_percent_value'] = 	$default_ssf_percent_value;
		$result['default_ssf_value'] = 	$default_ssf_value;
		$result['ssf_percent_value_pair'] = 	$ssf_percent_value_pair;

		return $result;
	}
	
	//Calc SSF
	function cal_ssf($ssf_cat,$gross_pay)
	{			
		//SSFs
		if(is_numeric($ssf_cat))
		{
			//Get ssf Components
			$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
			if($ssf_items->num_rows() > 0)
			{
				$ssf_comp_array = array();
				foreach($ssf_items->result() as $row)
				{
					//$row->order
					$percent_value = $row->percent_value;
					$ssf_comp_array[] = ($gross_pay*($percent_value/100));
				}
				return $ssf_comp_array;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	
	//SSFs Percent and Value Pair
	function cal_ssf_percent_value_pair($ssf_cat,$gross_pay)
	{			
		//SSFs
		if(is_numeric($ssf_cat))
		{
			//Get ssf Components
			$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
			if($ssf_items->num_rows() > 0)
			{
				$ssf_comp_array = array();
				foreach($ssf_items->result() as $row)
				{
					//$row->order
					$percent_value = $row->percent_value;
					$ssf_comp_array[$percent_value] = ($gross_pay*($percent_value/100));
				}
				return $ssf_comp_array;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	
	//SSFs Percent and Value Pair presentation
	function cal_ssf_percent_value_pair_presentation($ssf_cat,$gross_pay)
	{			
		//SSFs
		if(is_numeric($ssf_cat))
		{
			//Get ssf Components
			$ssf_items = $this->SSF_config->get_ssf_presentation($ssf_cat);
			if($ssf_items->num_rows() > 0)
			{
				$ssf_comp_array = array();
				foreach($ssf_items->result() as $row)
				{
					//$row->order
					$percent_value = $row->percent_value;
					$ssf_comp_array[$percent_value] = ($gross_pay*($percent_value/100));
				}
				return $ssf_comp_array;
			}
			else
			{
				return 0;
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	
	
	//Default ssf info
	function default_ssf_percent_value($ssf_cat)
	{			
		if(is_numeric($ssf_cat))
		{
			//Net Basic Wage
			$ssf = $this->SSF_config->get_info($ssf_cat);
			$ssf_net_wage = $ssf->start; //Geting the dedault ssf value to cal the net wage
			$query_row = $this->SSF_config->get_default_ssf($ssf_net_wage);
			$percent_netwage = 0;
			$percent_value = 0;
			if($query_row)// Default exist 
			{
				$percent_value = $query_row->percent_value;
				return $percent_value;
			}
			else // Use next available value
			{
				$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
				if($ssf_items->num_rows() > 0)
				{
					foreach($ssf_items->result() as $row)
					{		
						$percent_value = $row->percent_value;
						break;						
					}
					return $percent_value;
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Getting the default SSF value
	function cal_default_ssf($ssf_cat,$gross_pay)
	{			
		if(is_numeric($ssf_cat))
		{
			//Net Basic Wage
			$ssf = $this->SSF_config->get_info($ssf_cat);
			$ssf_net_wage = $ssf->start; //Geting the dedault ssf value to cal the net wage
			$query_row = $this->SSF_config->get_default_ssf($ssf_net_wage);
			$percent_netwage = 0;
			$percent_value = 0;
			if($query_row)// Default exist 
			{
				$percent_value = $query_row->percent_value;
				$percent_netwage = ($gross_pay*($percent_value/100));
				return $percent_netwage;
			}
			else // Use next available value
			{
				$ssf_items = $this->SSF_config->get_ssf_coponents($ssf_cat);
				if($ssf_items->num_rows() > 0)
				{
					foreach($ssf_items->result() as $row)
					{		
						$percent_value = $row->percent_value;
						$percent_netwage = ($gross_pay*($percent_value/100));
						break;						
					}
					return $percent_netwage;
				}
				else
				{
					return 0;
				}
			}
		}
		else
		{
			return 0;
		}
	}
	
	
	//Calc Net Basic Wage
	function cal_net_basic_wage($basic_wages,$default_ssf_value)
	{			
		//Net Basic Wage
		$net_basic_wage = $basic_wages - $default_ssf_value;
		
		return $net_basic_wage;
	}
	
	
	//Calc Gross Pay
	function cal_gross_pay($basic_wages,$total_allowances)
	{					
		return $basic_wages + $total_allowances;
	}
	
	//Tax
	function cal_tax($tax_cat,$taxable_income)
	{
		if(is_numeric($tax_cat))
		{
			$tax = $this->Tax_config->get_info($tax_cat);
			$exceeding = $tax->exceeding; 
		
			$tax_items = $this->Tax_config->get_tax_coponents($tax_cat);
			if($tax_items->num_rows() > 0)
			{
				if($taxable_income >= $tax_items->row()->amount)//Taxable income must be grater than the very first entry
				{
					$percent_tax = array();
					$percent_value = 0;
					foreach($tax_items->result() as $row)
					{
						$row_amount = $row->amount;
						if($row_amount <= $taxable_income)
						{
							$percent_value = $row->percent_value;
							$percent_tax[] = ($row_amount*($percent_value/100));
							$taxable_income = $taxable_income - $row_amount;
						}
						else
						{
							$percent_value = $row->percent_value;
							$percent_tax[] = ($taxable_income*($percent_value/100));
							$taxable_income = $taxable_income - $taxable_income;
							break;
						}
					}
					// Now Exceeding
					if($taxable_income > 0)
					{
						$percent_tax[] = ($taxable_income*($exceeding/100));
					}
		
					//Sum them all
					return number_format(array_sum($percent_tax), 2, '.', '');
				}
				else // No tax At all
				{
					return 0;
				}
			}
			else // Only Exceeding 
			{
				if($taxable_income > 0)
				{
					$only_exceeding = ($taxable_income*($exceeding/100));
				}
				return number_format($only_exceeding, 2, '.', '');
			}
		}
		else
		{
			return 0;
		}
		
	}
	
	//Taxable Income
	function cal_taxable_income($gross_pay,$default_ssf_value)
	{					
		return $gross_pay - $default_ssf_value;
	}
	
	//Net Pay
	function cal_net_pay($taxable_income,$total_payroll_deductions)
	{					
		return $taxable_income - $total_payroll_deductions;
	}
	
	//total_payroll_deductions
	function cal_total_payroll_deductions($tax,$deduction_totals)
	{					
		return  $tax + $deduction_totals;
	}
	
	//Total SSF Payable
	function cal_total_ssf_payable($ssf_array)
	{					
		if(is_array($ssf_array))
		{
			return array_sum($ssf_array);
		}
		else
		{
			return $ssf_array;
		}
		
	}
	
	/////////////////////////////////*************************************////////////////////////////////////
	
	
	/*
	//Summary sales report
	function summary_sales($start_date, $end_date)
	{
		$this->load->model('reports/Summary_sales');
		$model = $this->Summary_sales;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date));
		
		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['sale_date'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')));
		}

		$data = array(
			"title" => $this->lang->line('reports_sales_summary_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"data" => $tabular_data,
			"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date))
		);

		$this->load->view("reports/tabular",$data);
	}
	
	//Summary categories report
	function summary_categories($start_date, $end_date)
	{
		$this->load->model('reports/Summary_categories');
		$model = $this->Summary_categories;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date));
		
		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['category'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')));
		}

		$data = array(
			"title" => $this->lang->line('reports_categories_summary_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"data" => $tabular_data,
			"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date))
		);

		$this->load->view("reports/tabular",$data);
	}
	
	//Summary customers report
	function summary_customers($start_date, $end_date)
	{
		$this->load->model('reports/Summary_customers');
		$model = $this->Summary_customers;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date));
		
		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['customer'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')));
		}

		$data = array(
			"title" => $this->lang->line('reports_customers_summary_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"data" => $tabular_data,
			"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date))
		);

		$this->load->view("reports/tabular",$data);
	}
	
	//Summary suppliers report
	function summary_suppliers($start_date, $end_date)
	{
		$this->load->model('reports/Summary_suppliers');
		$model = $this->Summary_suppliers;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date));
		
		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['supplier'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')));
		}

		$data = array(
			"title" => $this->lang->line('reports_suppliers_summary_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"data" => $tabular_data,
			"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date))
		);

		$this->load->view("reports/tabular",$data);
	}
	
	//Summary items report
	function summary_items($start_date, $end_date)
	{
		$this->load->model('reports/Summary_items');
		$model = $this->Summary_items;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date));
		
		foreach($report_data as $row)
		{
			$tabular_data[] = array(character_limiter($row['name'], 16), to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')));
		}

		$data = array(
			"title" => $this->lang->line('reports_items_summary_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"data" => $tabular_data,
			"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date))
		);

		$this->load->view("reports/tabular",$data);
	}
	
	//Summary employees report
	function summary_employees($start_date, $end_date)
	{
		$this->load->model('reports/Summary_employees');
		$model = $this->Summary_employees;
		$tabular_data = array();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date));
		
		foreach($report_data as $row)
		{
			$tabular_data[] = array($row['employee'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')));
		}

		$data = array(
			"title" => $this->lang->line('reports_employees_summary_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"data" => $tabular_data,
			"summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date))
		);

		$this->load->view("reports/tabular",$data);
	}
	
	function specific_customer_input()
	{
		$data = $this->_get_common_report_data();
		$data['specific_input_name'] = $this->lang->line('reports_customer');
		
		$customers = array();
		foreach($this->Customer->get_all()->result() as $customer)
		{
			$customers[$customer->person_id] = $customer->first_name .' '.$customer->last_name;
		}
		$data['specific_input_data'] = $customers;
		$this->load->view("reports/specific_input",$data);	
	}

	function specific_customer($start_date, $end_date, $customer_id)
	{
		$this->load->model('reports/Specific_customer');
		$model = $this->Specific_customer;
		
		$headers = $model->getDataColumns();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'customer_id' =>$customer_id));
		
		$summary_data = array();
		$details_data = array();
		
		foreach($report_data['summary'] as $key=>$row)
		{
			$summary_data[] = array(anchor('sales/receipt/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target' => '_blank')), $row['sale_date'], $row['items_purchased'], $row['employee_name'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')), $row['payment_type'], $row['comment']);
			
			foreach($report_data['details'][$key] as $drow)
			{
				$details_data[$key][] = array($drow['name'], $drow['category'], $drow['quantity_purchased'], to_currency($drow['subtotal'],$this->config->item('currency_inuse')), to_currency($drow['total'],$this->config->item('currency_inuse')), to_currency($drow['tax'],$this->config->item('currency_inuse')),to_currency($drow['profit'],$this->config->item('currency_inuse')), $drow['discount_percent'].'%');
			}
		}

		$customer_info = $this->Customer->get_info($customer_id);
		$data = array(
			"title" => $customer_info->first_name .' '. $customer_info->last_name.' '.$this->lang->line('reports_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"summary_data" => $summary_data,
			"details_data" => $details_data,
			"overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date,'customer_id' =>$customer_id)),
		);

		$this->load->view("reports/tabular_details",$data);
	}
	
	function specific_employee_input()
	{
		$data = $this->_get_common_report_data();
		$data['specific_input_name'] = $this->lang->line('reports_employee');
		
		$employees = array();
		foreach($this->Employee->get_all()->result() as $employee)
		{
			$employees[$employee->person_id] = $employee->first_name .' '.$employee->last_name;
		}
		$data['specific_input_data'] = $employees;
		$this->load->view("reports/specific_input",$data);	
	}

	function specific_employee($start_date, $end_date, $employee_id)
	{
		$this->load->model('reports/Specific_employee');
		$model = $this->Specific_employee;
		
		$headers = $model->getDataColumns();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date, 'employee_id' =>$employee_id));
		
		$summary_data = array();
		$details_data = array();
		
		foreach($report_data['summary'] as $key=>$row)
		{
			$summary_data[] = array(anchor('sales/receipt/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target' => '_blank')), $row['sale_date'], $row['items_purchased'], $row['customer_name'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')), $row['payment_type'], $row['comment']);
			
			foreach($report_data['details'][$key] as $drow)
			{
				$details_data[$key][] = array($drow['name'], $drow['category'], $drow['quantity_purchased'], to_currency($drow['subtotal'],$this->config->item('currency_inuse')), to_currency($drow['total'],$this->config->item('currency_inuse')), to_currency($drow['tax'],$this->config->item('currency_inuse')),to_currency($drow['profit'],$this->config->item('currency_inuse')), $drow['discount_percent'].'%');
			}
		}

		$employee_info = $this->Employee->get_info($employee_id);
		$data = array(
			"title" => $employee_info->first_name .' '. $employee_info->last_name.' '.$this->lang->line('reports_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"summary_data" => $summary_data,
			"details_data" => $details_data,
			"overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date,'employee_id' =>$employee_id)),
		);

		$this->load->view("reports/tabular_details",$data);
	}
	
	function detailed_sales($start_date, $end_date)
	{
		$this->load->model('reports/Detailed_sales');
		$model = $this->Detailed_sales;
		
		$headers = $model->getDataColumns();
		$report_data = $model->getData(array('start_date'=>$start_date, 'end_date'=>$end_date));
		
		$summary_data = array();
		$details_data = array();
		
		foreach($report_data['summary'] as $key=>$row)
		{
			$summary_data[] = array(anchor('sales/receipt/'.$row['sale_id'], 'POS '.$row['sale_id'], array('target' => '_blank')), $row['sale_date'], $row['items_purchased'], $row['employee_name'], $row['customer_name'], to_currency($row['subtotal'],$this->config->item('currency_inuse')), to_currency($row['total'],$this->config->item('currency_inuse')), to_currency($row['tax'],$this->config->item('currency_inuse')),to_currency($row['profit'],$this->config->item('currency_inuse')), $row['payment_type'], $row['comment']);
			
			foreach($report_data['details'][$key] as $drow)
			{
				$details_data[$key][] = array($drow['name'], $drow['category'], $drow['quantity_purchased'], to_currency($drow['subtotal'],$this->config->item('currency_inuse')), to_currency($drow['total'],$this->config->item('currency_inuse')), to_currency($drow['tax'],$this->config->item('currency_inuse')),to_currency($drow['profit'],$this->config->item('currency_inuse')), $drow['discount_percent'].'%');
			}
		}

		$data = array(
			"title" =>$this->lang->line('reports_detailed_sales_report'),
			"subtitle" => date('m/d/Y', strtotime($start_date)) .'-'.date('m/d/Y', strtotime($end_date)),
			"headers" => $model->getDataColumns(),
			"summary_data" => $summary_data,
			"details_data" => $details_data,
			"overall_summary_data" => $model->getSummaryData(array('start_date'=>$start_date, 'end_date'=>$end_date)),
		);

		$this->load->view("reports/tabular_details",$data);
	}
	*/
}
?>